
const app = getApp();
let QQMapWX = require('../../lib/qqmap-wx-jssdk.js');
var qqmapsdk;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isCollected:false,  // 收藏
    sid: '',
    shop: ''
  },
  // 收藏功能
  handleCollection:function(){
    let isCollected = !this.data.isCollected
    console.log("收藏成功")
    this.setData({
      isCollected
    })
    //提示用户
    wx.showToast({
      title: isCollected ? '收藏成功':'取消收藏',
      icon:"success"
    })
  },

  makephone: function (e) {
    var that = this;
    // var addss = that.data.saddress;
    // console.log("address:" + e)
    qqmapsdk = new QQMapWX({
      key: 'NXTBZ-RES6W-KR7RT-RC2CP-XW4DF-UCBLZ'
    });
    qqmapsdk.geocoder({
      address: that.data.shop.address,
      success: function (res) {
        var lat = res.result.location.lat
        var lng = res.result.location.lng
        let title = that.data.shop.dname;
        wx.getSetting({
          success(res) {
            if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
              //表示非初始化进入该页面 且未授权：
              wx.showModal({
                title: '请求授权当前位置',
                content: '需要获取您的地理位置，请确认授权',
                showCancel: true,
                cancelText: '取消',
                cancelColor: '#000000',
                confirmText: '确定',
                confirmColor: '#3CC51F',
                success: (result) => {
                  if (res.cancel) {
                    wx.showToast({
                      title: '拒绝授权',
                      icon: 'none',
                      duration: 1000
                    });
                  } else if (result.confirm) {
                    //调起客户端小程序设置界面，返回用户设置的操作结果。 
                    //设置界面只会出现小程序已经向用户请求过的权限
                    wx.openSetting({
                      success: (dataAu) => {
                        if (dataAu.authSetting["scope.userLocation"] == true) {
                          wx.showToast({
                            title: '授权成功',
                            icon: 'success',
                            duration: 1000
                          });
                          //再次授权之后，调用腾讯位置服务的路线规划插件API
                          that.callRoutePlanPlugin()
                        } else {
                          wx.showToast({
                            title: '授权失败',
                            icon: 'none',
                            duration: 1000
                          });
                        }
                      }
                    });

                  }
                }
              });

            } else if (res.authSetting['scope.userLocation'] == undefined) {
              //调用腾讯位置服务的路线规划插件API
              that.callRoutePlanPlugin(lat, lng, title)
            } else {
              //调用腾讯位置服务的路线规划插件API
              that.callRoutePlanPlugin(lat, lng, title)
            }
          }

        })
      },
      fail: function (res) {
        console.log(res);
      },
      complete: function (res) {
        console.log(res);
      }
    });
  },

  callRoutePlanPlugin(lat, lng, title) {
    var that = this;
    console.log("lat:" + lat + " lng:" + lng + "title:" + title)
    let key = 'NXTBZ-RES6W-KR7RT-RC2CP-XW4DF-UCBLZ';  //使用在腾讯位置服务申请的key
    let referer = '理发';   //调用插件的app的名称
    let endPoint = JSON.stringify({  //终点
      'name': title,
      'latitude': lat,
      'longitude': lng
    });
    wx.navigateTo({
      url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.data.sid = options.id;
    that.data.dname = options.dname;
    qqmapsdk = new QQMapWX({
      key: 'NXTBZ-RES6W-KR7RT-RC2CP-XW4DF-UCBLZ'
    })
    wx.request({
      url: app.globalData.headurl + '/Lfdlifashi/selectLfdlifashiBySid',
      data: {
        sid: options.id
      },
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=utf-8",
        //token: app.globalData.token
      },
      success: function (r) {
        that.setData({
          lfdlifashilist: r.data
        })
      }
    })

    wx.request({
      url: app.globalData.headurl + '/Lfdshop/selectLfdshopById',
      data: {
        id: options.id
      },
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=utf-8",
        //token: app.globalData.token
      },
      success: function (r) {
        that.data.shop = r.data;
        that.setData({
          shop:r.data
        })
      }
    })

    wx.request({
      url: app.globalData.headurl + '/Ifc/selectIfcBySid',
      data: {
        sid: options.id
      },
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=utf-8",
        //token: app.globalData.token
      },
      success: function (r) {
        that.setData({
          Ifclist: r.data
        })
      }
    })


  },

  yuyue: function (e) {
    var lid = e.currentTarget.dataset.lid;
    var feiyong = e.currentTarget.dataset.feiyong;
    var name = e.currentTarget.dataset.name;
    var dname = e.currentTarget.dataset.dname;
    // console.log("美发店",dname)
    // console.log("发型师",name)
    // console.log("费用:",feiyong)

    var that = this;
    wx.request({
      url: app.globalData.headurl + '/qcustomer/getcustomerOpenid',
      data: {
        openid: app.globalData.openid
      },
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=utf-8",
      },
      success: function (re) {
        if (re.data == null) {
          wx.showToast({
            title: '请先注册',
          })
        } else {
          console.log("用户存在")
          if (parseInt(re.data.money) < parseInt(feiyong)) {
            console.log("余额不足")
            wx.showToast({
              title: '余额不足',
            })
          } else {
            console.log("余额充足" + feiyong)
            wx.navigateTo({
              url: '../addyuyue/index?lid=' + lid +  '&dname=' + that.data.dname + 
              '&name='+name+ '&feiyong=' + feiyong + '&sid=' + that.data.sid,
            })
          }

        }
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.fixData();
    // console.log(dname)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  fixData: function () {

    this.setData({
      appointments: this.data.appointments,
    })
  },

  btnClickReminder: function (e) {
    var lid = e.currentTarget.dataset.lid;
    var fname = e.currentTarget.dataset.fname;
    // console.log("lid:" + lid)
    // console.log("理发师名字:" + fname)
    wx.navigateTo({
      url: '../appointmentDetail/appointmentDetail?lid=' + lid + '&dname=' + this.data.dname+ '&fname=' + fname
    })
  }
})